// 上面这个代码处理过度动画（默认加上不用管）
document.addEventListener('DOMContentLoaded', () => {
  setTimeout(() => {
    document.body.classList.add('sidenav-pinned')
    document.body.classList.add('ready')
  }, 200)
})

// axios相关配置
axios.defaults.baseURL = 'http://ajax-api.itheima.net/'

 // 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  const token = localStorage.getItem('user-token')               
  if (token) config.headers.Authorization = token
  console.log(config)
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response;
}, function (error) {
  // 对响应错误做点什么
  console.dir(error)
  if (error.response.status === 401){
    localStorage.removeItem('user-token')
    localStorage.removeItem('username')
    location.href = './login.html'
  }
  return Promise.reject(error);
});

// 用户名显示和退出功能
const username = document.querySelector('#username')
if(username) {
  username.innerHTML = localStorage.getItem('username')
}

const logout = document.querySelector('#logout')
if (logout){
  logout.addEventListener('click',function () {
    localStorage.removeItem('user-token')
    localStorage.removeItem('username')
    location.href = './login.html'
  })
}

// 轻提示组件的封装
      const toastBox = document.querySelector('#myToast')
      const toast = new bootstrap.Toast(toastBox,{
        animation: true,
        autohide: true,
        delay:2000
      })
      
      const tips = (msg) => {
        document.querySelector('.toast-body').innerHTML = msg
        toast.show()
      }